package com.heima.controller.sku;

import com.heima.common.AliOssUtil;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.UUID;

@RestController
@RequestMapping("/api/vm-service/sku")
public class FileUploadController {

    @Value("${aliyun.oss.endpoint}")
    private String endpoint;
    @Value("${aliyun.oss.accessKeyId}")
    private String accessKeyId;
    @Value("${aliyun.oss.accessKeySecret}")
    private String accessKeySecret;
    @Value("${aliyun.oss.bucketName}")
    private String bucketName;
    @PostMapping("/fileUpload")
    public String fileUpload(@RequestParam("fileName") MultipartFile fileName) throws IOException {
        String originalFilename = fileName.getOriginalFilename();
        String fileNames = UUID.randomUUID() + originalFilename.substring(originalFilename.lastIndexOf("."));
        AliOssUtil aliOssUtil = new AliOssUtil(endpoint,accessKeyId,accessKeySecret,bucketName);
        return aliOssUtil.upload(fileName.getBytes(), fileNames);
    }

}
